<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools>
        <el-button type="primary" size="small" @click="openDialog(1,0)">添加权限</el-button>
      </page-tools>
      <!-- 梯形表格 -->
      <el-table :data="list" border="" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <el-button v-if="row.type * 1 === 1" type="text" @click="openDialog( 2,row.id)">添加</el-button>
            <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(row.id)"> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹窗 -->
    <AddPerm ref="addPerm" @updateList="getPermissionList" /></div>
</template>

<script>
import { getPermissionListApi, delPermissionApi } from '@/api/permission'
import { tranListTreeToData } from '@/utils'
import AddPerm from '@/views/permission/components/addPerm.vue'
export default {
  name: 'Permission',
  components: { AddPerm },
  data() {
    return {
      list: []

    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    editPermission(id) {
      this.$refs.addPerm.getRowInfo(id)
    },
    async delPermission(id) {
      try {
        await this.$confirm('确认删除吗')
      } catch (error) {
        return console.log(error)
      }
      await delPermissionApi(id)
      this.$message.success('删除成功')
      this.getPermissionList()
    },

    openDialog(type, pid) {
      this.$refs.addPerm.dialogShow = true
      this.$refs.addPerm.formData.type = type
      this.$refs.addPerm.formData.pid = pid
    },
    async getPermissionList() {
      const res = await getPermissionListApi()
      this.list = tranListTreeToData(res, '0')
    }
  }
}
</script>

<style>

</style>
